from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
from django.db.models import Q
from utils.jwttool import tool
import re
import random
from django.core.cache import cache
from utils.sendmsm import send_message
import json


# Create your views here.
from user import models
class LoginPwd(APIView):
    def post(self,request):
        account=request.data.get("account","")
        password=request.data.get("password","")
        #1先判断是否为空
        if not account or not password:
            return Response({"msg":"账号或密码不能为空","code":400})
        #2判断账号是否存在 邮箱 手机号 用户名
        #select * from user where email=account or phone=account or username=account
        info=models.User.objects.filter(Q(email=account)|Q(phone=account)|Q(username=account),isdel=False).first()
        if not info:
            return Response({"msg":"账号不存在","code":400})
        #3判断密码是否正确
        if info.password !=password:
            return Response({"msg":"密码错误","code":400})
        #4验证通过 生成token
        token=tool.gettoken(info.id)
        #5返回结果
        return Response({"msg":"登录成功","code":200,"data":{"token":token,"user":info.id}})

#发送短信验证码视图
class SendCode(APIView):
    def get(self,request):
        #1 获取手机号
        phone=request.query_params.get("phone")
        #2 验证手机号格式
        if not phone or not re.match(r"^1[3-9]\d{9}$",phone):
            return Response({"msg":"手机号格式不正确","code":400})
        #3 验证缓存中手机号是否存在 是否在冷却期  cache.set("手机号做为键",“1”,60*2) 读取 cache.get("手机号做为键")
        if cache.get(phone):
            return Response({"msg":"手机号在冷却期,请勿频繁发送","code":400})
        #4 生成验证码 生成一个4位随机数验证码
        code=str(random.randint(1000,9999))
        #5 发送短信
        #调用异步函数 然后将验证码存储在redis中
        res=tasks.send.delay(phone,code)

        cache.set(phone,code,60*10)
        return Response({"msg":"发送成功","code":200})


class  Login(APIView):
    def post(self,request):
        #1 获取到手机号和验证码
        phone=request.data.get("phone")
        code=request.data.get("code")
        #2 验证手机号和验证码格式
        if not phone or not code:
            return Response({"msg":"手机号或者验证码不能为空","code":400})
        #3 验证手机号和验证码是否匹配
        cachecode=cache.get(phone,"")#从缓存中拿到的验证码
        if str(cachecode)!=str(code):
            return Response({"msg":"手机号或者验证码不匹配","code":400})
        #4 验证手机号是否在数据库中存在 如果存在 返回用户信息  如果不在 创建用户信息
        userinfo=models.User.objects.filter(phone=phone).first()#有可能是空的哦
        if not userinfo:
            #创建用户
            userinfo=models.User.objects.create(phone=phone)
        #5 生成token
        token = tool.gettoken(userinfo.id)
        #6 返回token和用户信息
        return Response({"msg":"登录成功","code":200,"data":{"token":token,"user":userinfo.id}})

from utils.myauthorization import MyAuthentication
from user import tasks
class Test(APIView):
    #开启认证
    #authentication_classes = [MyAuthentication]#类写到列表或者元祖中
    def get(self,request):
        #调用异步任务
        result=tasks.send_email.delay("张小三")
        #获取到任务的id
        print(result.id)
        return Response({"msg":"测试成功","code":200})

class Register(APIView):
    def post(self,request):
        # 1 获取到用户名 邮箱 密码手机号 code
        #先校验code
        code=request.data.get("code")
        username=request.data.get("username")
        email=request.data.get("email")
        password=request.data.get("password")
        phone=request.data.get("phone")
        if not username or not email or not password or not phone or not code:
            return Response({"msg":"参数不完整,有空值","code":400})
        #2 验证用户名是否唯一
        info=models.User.objects.filter(username=username).first()
        if info:
            return Response({"msg":"用户名已存在","code":400})
        #3 验证邮箱是否符合格式并且唯一
        if not re.match(r'^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$',email):
            return Response({"msg":"邮箱格式不正确","code":400})
        info=models.User.objects.filter(email=email).first()
        if info:
            return Response({"msg":"邮箱已存在","code":400})
        #4 验证密码是否符合格式
        if not re.match(r'^[a-zA-Z0-9]{6,20}$',password):
            return Response({"msg":"密码格式不正确","code":400})
        #5 验证手机号是否符合格式并且是否为有效的手机号,如果手机号存在 提示用户直接登录
        if not re.match(r'^1[3456789]\d{9}$',phone):
            return Response({"msg":"手机号格式不正确","code":400})
        info=models.User.objects.filter(phone=phone).first()
        if info:
            return Response({"msg":"手机号已存在,请直接登录","code":400})
        #code的校验
        rediscode=cache.get(phone,"")
        if code!=rediscode:
            return Response({"msg":"验证码错误","code":400})
        #6 创建用户信息
        info=models.User.objects.create(username=username,email=email,phone=phone,password=password)
        return Response({"msg":"注册成功","code":200})

